package asay.and.tests;

import android.content.Context;
import android.test.AndroidTestCase;
import android.test.InstrumentationTestCase;
import android.view.View;
import cn.bmob.v3.Bmob;
import cn.bmob.v3.BmobInstallation;
import cn.bmob.v3.BmobQuery;
import cn.bmob.v3.BmobUser;
import cn.bmob.v3.listener.FindListener;
import cn.bmob.v3.listener.SaveListener;
import com.bmob.lostfound.bean.Found;
import com.bmob.lostfound.bean.Lost;
import com.bmob.lostfound.config.Constants;

import java.util.List;
import java.util.concurrent.Future;
import java.util.concurrent.FutureTask;

/**
 * Created by mac on 16/5/24.
 */
public class BmobTest extends AndroidTestCase {
    private Context context;

    @Override
    public void setUp() throws Exception {
        context = getContext();
        Bmob.initialize(context, Constants.Bmob_APPID);

    }

    public void test2seeInfo(){
        BmobInstallation ins = BmobInstallation.getCurrentInstallation(context);
        System.out.println(ins.getInstallationId());
        System.out.println(ins.getDeviceType());
        ins.save();

        BmobUser user = new BmobUser();
        user.setUsername("abc");
        user.setPassword("123");
        user.save(context, new SaveListener() {
            @Override
            public void onSuccess() {
                System.out.println("成功");
            }

            @Override
            public void onFailure(int i, String s) {
                System.out.println("失败");
            }
        });


        try {
            Thread.sleep(1000 * 3);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
    public void test1get() {



        queryLosts();
        try {
            Thread.sleep(1000 * 3);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    /**
     * 查询全部失物信息 queryLosts
     *
     * @return void
     * @throws
     */
    private void queryLosts() {
        BmobQuery<Lost> query = new BmobQuery<Lost>();
        query.order("-createdAt");// 按照时间降序
        assertNotNull(context);
        System.out.println(context);

        query.findObjects(context, new FindListener<Lost>() {
            public void onSuccess(List<Lost> losts) {
                System.out.println("size :" + losts.size());
                for (Lost l : losts) {
                    System.out.println(l.getTitle());
                }
            }

            @Override
            public void onError(int code, String arg0) {
                System.out.println("error...");
            }
        });
    }

}
